﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class MyUI : MonoBehaviour {
	private Dropdown GroundList;
	private Dropdown WallList;
	private Dropdown GoodsList;
	private Toggle NoneBtn;
	private Toggle GroundBtn;
	private Toggle WallBtn;
	private Toggle GoodsBtn;
	private Toggle DelBtn;
	void LoadGroundList()
	{
        uint nCount = DBCManager.GetInstance().GetGroundDBC().GetRecordCount( );
		string fileName;
        for(uint i = 0; i < nCount; i++)
        {
			fileName = DBCManager.GetInstance().GetGroundDBC().GetString(i, 0);
			Dropdown.OptionData op = new Dropdown.OptionData();
            op.text = fileName;
            GroundList.options.Add(op);
        }
	}
	void LoadWallList()
	{
        uint nCount = DBCManager.GetInstance().GetWoodenWallDBC().GetRecordCount( );
		string fileName;
        for(uint i = 0; i < nCount; i++)
        {
            fileName = DBCManager.GetInstance().GetWoodenWallDBC().GetString(i, 0);
			Dropdown.OptionData op = new Dropdown.OptionData();
            op.text = fileName;
            WallList.options.Add(op);
        }
	}
	void LoadGoodsList()
	{
        uint nCount = DBCManager.GetInstance().GetOtherDBC().GetRecordCount( );
		string fileName;
        for(uint i = 0; i < nCount; i++)
        {
            fileName = DBCManager.GetInstance().GetOtherDBC().GetString(i, 0);
			Dropdown.OptionData op = new Dropdown.OptionData();
            op.text = fileName;
            GoodsList.options.Add(op);
        }
	}
	void Start () {		
		GroundList = GameObject.Find("GroundList").GetComponent<Dropdown>();	GroundList.ClearOptions(); GroundList.onValueChanged.AddListener(OnGroundChange);
		WallList = GameObject.Find("WallList").GetComponent<Dropdown>();		WallList.ClearOptions(); WallList.onValueChanged.AddListener(OnWallChange);
		GoodsList = GameObject.Find("GoodsList").GetComponent<Dropdown>();		GoodsList.ClearOptions(); GoodsList.onValueChanged.AddListener(OnGoodsChange);
		
		NoneBtn = GameObject.Find("NoneBtn").GetComponent<Toggle>();	NoneBtn.onValueChanged.AddListener(OnSelNoneBtn);
		GroundBtn = GameObject.Find("GroundBtn").GetComponent<Toggle>();	GroundBtn.onValueChanged.AddListener(OnSelGroundBtn);
		WallBtn = GameObject.Find("WallBtn").GetComponent<Toggle>();	WallBtn.onValueChanged.AddListener(OnSelWallBtn);
		GoodsBtn = GameObject.Find("GoodsBtn").GetComponent<Toggle>();	GoodsBtn.onValueChanged.AddListener(OnSelGoodsBtn);
		DelBtn = GameObject.Find("DelBtn").GetComponent<Toggle>();	DelBtn.onValueChanged.AddListener(OnSelDelBtn);
		
		LoadGroundList();
		LoadWallList();
		LoadGoodsList();
	}
	private int m_curGround = 0;
	public void OnGroundChange(int value)
    {
		m_curGround = value;
		StateManager.GetInstance().ChangeState(ControlState.ecs_Ground, m_curGround);
	}
	private int m_curWall = 0;
	public void OnWallChange(int value)
    {
		m_curWall = value;
		StateManager.GetInstance().ChangeState(ControlState.ecs_BuildingWoodenWall, m_curWall);
	}
	private int m_curGoods = 0;
	public void OnGoodsChange(int value)
    {
		m_curGoods = value;
		StateManager.GetInstance().ChangeState(ControlState.ecs_Goods, m_curGoods);
	}
	public void OnSelNoneBtn(bool isOn)
	{
		if(isOn)
		{
			StateManager.GetInstance().ChangeState(ControlState.ecs_None, 0);
		}
	}
	public void OnSelGroundBtn(bool isOn)
	{
		if(isOn)
		{
			StateManager.GetInstance().ChangeState(ControlState.ecs_Ground, m_curGround);
		}
	}
	public void OnSelWallBtn(bool isOn)
	{
		if(isOn)
		{
			StateManager.GetInstance().ChangeState(ControlState.ecs_BuildingWoodenWall, m_curWall);
		}
	}
	public void OnSelGoodsBtn(bool isOn)
	{
		if(isOn)
		{
			StateManager.GetInstance().ChangeState(ControlState.ecs_Goods, m_curGoods);	
		}
	}
	public void OnSelDelBtn(bool isOn)
	{
		if(isOn)
		{
			StateManager.GetInstance().ChangeState(ControlState.ecs_Del, 0);
		}
	}
}